ASP Session
The Session Object in ASP is a great tool for the modern web site. It allows you
to keep information specific to each of your site's visitors. Information like username, shopping cart, and location can be stored for the life of the session
so you don't have to worry about passing information page to page.
In old web page designs you might have to try to pass information this information through HTML Forms or other methods.
ASP Session Object
Contained within the Session Object are several important features that we
will talk about in this lesson. The most important thing to know about ASP's Session Object
is that it is only created when you store information into the Session Contents collection. We will
now look into creating and storing information in an ASP Session.
ASP Session Variables
To store a Session Variable you must put it into the Contents collection, which is very easy to do.
If you have already read the ASP Arrays Lesson then
this bit of code will be a cinch!
Here we are saving the Time when someone visited this page into the Session Contents collection and then displaying it .
ASP Code:
<%
'Start the session and store information
Session("TimeVisited") = Time()
Response.Write("You visited this site at: " & Session("TimeVisited"))
%>
Display:
You visited this site at:
Here we are creating two things actually: a key and a value. Above we created the key
"TimeVisited" which we assigned the value returned by the Time() function. Whenever you create a Session Variable to be
stored in the Session Contents collection you will need to make this Key / Value pair.
ASP Session ID
The ASP Session ID is the unique identifier that is automatically created when a Session starts
for a given visitor. The Session ID is a property of the Session Object and is rightly called the SessionID property.
Below we store the user's SessionID into a variable.
ASP Code:
<%
Dim mySessionID
mySessionID = Session.SessionID
%>
ASP Session Timeout
A Session will not last forever, so eventually the data stored within the Session will be lost.
There are many reasons for a Session being destroyed. The user could close their browser or they could
leave their computer for an extended amount of time and the Session would time out. You can
set how long it takes, in minutes, for a session to time out with the Timeout property.
Below we set our session to timeout after 240 minutes, which should be more than enough time for most web sites.
ASP Code:
<%
Session.Timeout = 240
Response.Write("The timeout is: " & Session.Timeout)
%>
Display:
The timeout is: 240
Note: Timeout is defined in terms of minutes.
Found Something Wrong in this Lesson?Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time! |